class Solution {
public:
    bool check(vector<int>& nums,int p,int mx){
        int cnt=0;
            for(int i=0;i<nums.size()-1;++i){
                if(nums[i+1]-nums[i]<=mx){
                    cnt++;
                    i++;
                }
            }
            return cnt >=p;
    }
    int minimizeMax(vector<int>& nums, int p) {
        sort(nums.begin(),nums.end());
        int left =0,right=nums.back()-nums[0];
        while(left<right){
            int mid=(left+right)>>1;
            if(check(nums,p,mid)){
                right = mid;
            }else{
                left=mid+1;
            }
        }
        return left;
    }
};
